package com.cy.shiro.controller;

import com.cy.shiro.pojo.SysResources;
import com.cy.shiro.pojo.SysUser;
import com.cy.shiro.service.SysResourcesService;
import com.cy.shiro.util.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SysResourcesController {

    @Autowired
    private BaseController baseController;
    @Autowired
    private SysResourcesService sysResourcesService;
    //资源列表
    @RequestMapping("/resources")
    public String resources(Model model){
        //资源权限
        baseController.sysResources("资源管理","/resources", model);

        return "/resource/resources";
    }

    @GetMapping("/resources/list")
    @ResponseBody
    @CrossOrigin
    public Object list(HttpServletRequest request){
        String limit = request.getParameter("limit") == null ? "10" : request.getParameter("limit");
        String offset = request.getParameter("offset");
        String page = request.getParameter("page");
        String sort = "desc";

        if(limit == null){
            limit = "10";
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("limit", limit);
        map.put("offset", offset);
        map.put("page", page);
        map.put("sort", sort);
        map.put("name", request.getParameter("name"));
        map.put("url", request.getParameter("url"));
        map.put("permission", request.getParameter("permission"));

        PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(limit));

        List<SysResources> resourcesList = sysResourcesService.selectAllResoures(map);

        PageInfo<SysResources> pageInfo = new PageInfo<>(resourcesList);
        System.out.println("pageInfo:"+pageInfo.getTotal());

        int total = Integer.valueOf((int) pageInfo.getTotal());

        return  ResultUtil.bootstrapTableJson(pageInfo.getList(), total, map);
    }

    //获取可用菜单列表(menu)
    @PostMapping("/resources/listAllAvailableMenu")
    @ResponseBody
    @CrossOrigin
    public Object listAllAvailableMenu(){

        List<SysResources> selectAllResoures = sysResourcesService.listAllAvailableMenu();
        System.out.println("菜单列表:"+selectAllResoures);

        return ResultUtil.success("success", selectAllResoures);
    }

    //新增资源
    @PostMapping("/resources/addPost")
    @ResponseBody
    @CrossOrigin
    public Object addPost(SysResources sysResources){
        //接收参数
        System.out.println("新增资源：" + sysResources);

        Integer id = sysResourcesService.addResources(sysResources);
        if("0".equals(String.valueOf(id)) || "null".equals(String.valueOf(id))){
            return ResultUtil.error("新增失败");
        }

        return ResultUtil.success("新增成功");
    }

    //编辑资源
    @PostMapping("/resources/editPost")
    @ResponseBody
    @CrossOrigin
    public Object editPost(SysResources sysResources){
        //接收参数
        System.out.println("编辑信息:"+sysResources);

        Integer id = sysResourcesService.editResources(sysResources);
        if("0".equals(String.valueOf(id)) || "null".equals(String.valueOf(id))){
            return ResultUtil.error("编辑失败");
        }

        return ResultUtil.success("编辑成功");
    }

    //删除资源
    @PostMapping("/resources/delResources")
    @ResponseBody
    @CrossOrigin
    public Object delResources(HttpServletRequest request){
        //接收参数
        String id = request.getParameter("id");

        Integer integer = sysResourcesService.delResources(Integer.valueOf(id));

        if("0".equals(String.valueOf(integer)) || "null".equals(String.valueOf(integer))){
            return ResultUtil.error("删除失败");
        }

        return ResultUtil.success("删除成功");
    }

    //获取权限资源列表
    @PostMapping("/resources/queryResourcesListWithSelected")
    @ResponseBody
    @CrossOrigin
    public Object queryResourcesListWithSelected(Integer id){


        List<Map<String, Object>> resourcesList = sysResourcesService.queryResourcesListWithSelected(id);
        System.out.println(resourcesList);
        return ResultUtil.success("success",resourcesList);
    }
}
